/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.training;

import java.util.LinkedList;

/**
 * This class represents the statistics in trainingszenarios. In general it
 * manages a LinkedList of TrainingStatsElement.
 */
public class TrainingStatistics {

	private LinkedList<TrainingStatsElement> elements;

	// constructor
	public TrainingStatistics() {
		super();
		this.elements = new LinkedList();
		// counter for training runs is always added
		this.addElement(0);
	}

	// adds a new Counter to the statistics
	public void addElement(int index) {
		// only add the element, if it isn't already in use
		if (indexInList(index) == -1) {
			this.elements.add(new TrainingStatsElement(index));
		}
	}

	// increases the counter with the index n
	public void incElement(int index) {
		int place = indexInList(index);
		// first we need to check if we have an element with such an index
		if (!(place == -1)) {
			this.elements.get(place).inc();
		}
	}

	// returns a training-counter
	public TrainingStatsElement getElement(int place) {
		if ((place > -1) && (place < this.elements.size())) {
			return this.elements.get(place);
		}
		System.err
				.println("[TrainingStatistics] Try to get not existant element");
		return null;
	}

	// returns the array of all training stats
	public TrainingStatsElement[] getElementsArray() {
		int s = this.elements.size();
		TrainingStatsElement[] els = new TrainingStatsElement[s];
		for (int i = 0; i < s; i++) {
			els[i] = (TrainingStatsElement) (this.elements.get(i));
		}
		return els;
	}

	// _____________________________________________________________
	// HELP METHODS
	// _____________________________________________________________

	private int indexInList(int index) {
		for (int i = 0; i < this.elements.size(); i++) {
			if (this.elements.get(i).getIndex() == index)
				return i;
		}
		return -1;
	}
}